package com.xkdx.guangguang.module.network;

import java.nio.charset.Charset;
import java.util.HashMap;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;

import android.R.integer;
import android.annotation.SuppressLint;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;

import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.module.util.ErrorModule;

@SuppressLint("ParserError")
public class IOHttpTask extends AsyncTask<String, integer, HashMap<String, AbsModule>> {

	private AbsAction actions;
	private AbsModule module;
	protected BaseFragment baseFragment;
	protected IOHttpRequest requestInstent;
	protected HashMap<String, AbsModule> map;
	protected int connectTimeout = 3000, soTimeOut = 3000;
	protected Context context;
	protected boolean mUseCache = true;
	protected int mCacheEffective = 360;

	@Override
	protected HashMap<String, AbsModule> doInBackground(String... str) {
		map = new HashMap<String, AbsModule>();
		if (!isOpenNetwork()) {
			map.put("1", new ErrorModule("未有打开的网络，请稍后重新刷新"));
			return map;
		}

		try {
			requestInstent.setRequestContent(actions.requestData);
			requestInstent.setmConnectionTimeOut(connectTimeout);
			requestInstent.setmSocketTimeOut(soTimeOut);

			HttpPost mRequest = new HttpPost(actions.url);
			actions.constructRequest();

			MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

			reqEntity.addPart("JsonString", new StringBody((String) actions.requestData, Charset.forName("UTF-8")));
			switch (actions.key) {
			case 1:
				reqEntity.addPart("Photo", actions.fileBody);
				break;
			case 2:
				reqEntity.addPart("Picture", actions.fileBody);
				break;
			case 3:
				reqEntity.addPart("UserAvatar", actions.fileBody);
				break;
			case 4:
//				reqEntity.addPart("Picture", actions.editFileBody);
				reqEntity.addPart("Cover", actions.editeCutFileBody);
				break;
			default:
				break;
			}
			reqEntity.getContentEncoding();

			mRequest.setEntity(reqEntity);

			// mRequest.setEntity(new StringEntity("JsonString="+(String)
			// actions.requestData,HTTP.UTF_8));

			requestInstent.setHttpRequest(mRequest);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("1", new ErrorModule("请求设置错误"));
			return map;
		}

		try {
			requestInstent.execute(mUseCache, mCacheEffective);
		} catch (ConnectTimeoutException e) {
			e.printStackTrace();
			map.put("1", new ErrorModule("连接超时"));
			return map;
		} catch (Exception e) {
			Log.e("lhl", "login", e);
			e.printStackTrace();
			map.put("1", new ErrorModule("您的网络不好"));
			return map;
		}

		try {
			module.setMyObject(requestInstent.backResult);
			module.parseData();
		} catch (Exception e) {
			e.printStackTrace();
			map.put("1", new ErrorModule("解析数据错误"));
			return map;
		}
		map.put("0", module);
		return map;
	}

	public void setActitons(AbsAction actions) {
		this.actions = actions;
	}

	public void setModule(AbsModule module) {
		this.module = module;
	}

	private boolean isOpenNetwork() {
		try {

			ConnectivityManager connectivity = (ConnectivityManager) baseFragment.getActivity().getSystemService(
					Context.CONNECTIVITY_SERVICE);

			if (connectivity != null) {
				NetworkInfo info = connectivity.getActiveNetworkInfo();
				if (info != null && info.isConnected()) {
					if (info.getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

}
